home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / programr / stdg44.exe / lha / EVENTS2.C < prev    next >
C/C++ Source or Header  |  1994-01-10  |  4KB  |  168 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "stdg.h"
  4.  
  5. /*
  6.  * This program displays mouse, keyboard, resize and close events
  7.  * as they happen. It uses an offscreen bitmap which allows redrawing
  8.  * of the window when it is resized.
  9.  */
  10.  
  11. short enabled = Enabled;
  12. menuitem file_menu[] = {
  13.     { "File",  0,  &enabled, NULL },
  14.     { "Quit", 'Q', &enabled, &gexit },
  15.     { NULL,    0,  NULL,     NULL }
  16. };
  17. menu menu_bar[] = {
  18.     file_menu, NULL
  19. };
  20.  
  21. char buf[256];
  22.  
  23. void scrollup(bitmap *b, long h)    /* scroll a bitmap upwards h pixels */
  24. {
  25.     rectangle sr = b->r;            /* source rectangle */
  26.     point dp = b->r.min;            /* destination point */
  27.  
  28.     sr.min.y += h;
  29.  
  30.     bit_copy(b, dp, b, sr, S);
  31.  
  32.     sr.min.y = sr.max.y - h;
  33.     fill_rect(b, sr, WHITE);        /* erase last line of text */
  34. }
  35.  
  36. void drawstr(window *w, char *str)  /* draw string at bottom of window */
  37. {
  38.     point dp;
  39.     bitmap *b;
  40.  
  41.     b = (bitmap *) w->data;         /* draw into offscreen bitmap */
  42.     if (b == NULL)                  /* error - so use window bitmap */
  43.         b = w->b;
  44.  
  45.     scrollup(b, fixed_font->height);
  46.  
  47.     dp = pt(3, b->r.max.y - fixed_font->height);
  48.  
  49.     draw_string(b, dp, fixed_font, str, BLACK);
  50. }
  51.  
  52. void showbitmap(window *w)          /* show offscreen bitmap on window */
  53. {
  54.     bitmap *b;
  55.     rectangle sr;
  56.  
  57.     b = (bitmap *) w->data;
  58.  
  59.     if (b != NULL) {
  60.         sr = b->r;
  61.         sr.min.y = sr.max.y - w->b->r.max.y;    /* show bitmap bottom */
  62.         bit_copy(w->b, pt(0,0), b, sr, S);
  63.     }
  64. }
  65.  
  66. void close(window *w)               /* handle click in close box */
  67. {
  68.     sprintf(buf, "Close: Attempt to close the window with close box.");
  69.     drawstr(w, buf);
  70.     showbitmap(w);
  71. }
  72.  
  73. void resize(window *w)              /* resize the offscreen bitmap */
  74. {
  75.     if (w->data == NULL)
  76.         w->data = new_bitmap(screen->r, 1);
  77.  
  78.     sprintf(buf, "Resize: r=(%ld,%ld,%ld,%ld)", w->r);
  79.     drawstr(w, buf);
  80. }
  81.  
  82. void redraw(window *w)              /* redraw window from offscreen */
  83. {
  84.     showbitmap(w);
  85. }
  86.  
  87. void printchar(window *w, ushort c) /* print out a keyboard char message */
  88. {
  89.     if ((c >= 0x0020) && (c <= 0x00FF))
  90.         sprintf(buf, "Key: hex=0x%4.4X char='%c'", c, c);
  91.     else
  92.         sprintf(buf, "Key: hex=0x%4.4X", c);
  93.  
  94.     drawstr(w, buf);
  95.     showbitmap(w);
  96. }
  97.  
  98. void printmouse(window *w, mouse m) /* print out a mouse message */
  99. {
  100.     sprintf(buf, "Mouse: xy= (%3ld, %3ld) buttons= ", m.xy.x, m.xy.y);
  101.  
  102.     if (m.buttons & LeftButton)
  103.         strcat(buf, "L");
  104.     else
  105.         strcat(buf, "-");
  106.     if (m.buttons & MiddleButton)
  107.         strcat(buf, "M");
  108.     else
  109.         strcat(buf, "-");
  110.     if (m.buttons & RightButton)
  111.         strcat(buf, "R");
  112.     else
  113.         strcat(buf, "-");
  114.  
  115.     strcat(buf, "  kind= ");
  116.  
  117.     if (m.kind == MouseMove)    strcat(buf, "MouseMove");
  118.     if (m.kind == MouseTimer)   strcat(buf, "MouseTimer");
  119.     if (m.kind & DoubleClick)   strcat(buf, "DoubleClick | ");
  120.     if (m.kind & MouseDown)     strcat(buf, "MouseDown | ");
  121.     if (m.kind & MouseUp)       strcat(buf, "MouseUp | ");
  122.  
  123.     if (m.kind & LeftButton) {
  124.         strcat(buf, "LeftButton");
  125.         if ((m.kind & MiddleButton) || (m.kind & RightButton))
  126.             strcat(buf, " | "); /* put separating OR sign in */
  127.     }
  128.     if (m.kind & MiddleButton) {
  129.         strcat(buf, "MiddleButton");
  130.         if (m.kind & RightButton)
  131.             strcat(buf, " | "); /* put separating OR sign in */
  132.     }
  133.     if (m.kind & RightButton)
  134.         strcat(buf, "RightButton");
  135.  
  136.     drawstr(w, buf);
  137.     showbitmap(w);
  138. }
  139.  
  140. int main(int argc, char **argv)
  141. {
  142.     ushort c;
  143.     mouse m;
  144.     window *w;
  145.  
  146.     ginit("Sample", NULL, menu_bar);
  147.  
  148.     w = new_window("Event Look", rect(0,0,0,0),
  149.                     Closebox | Titlebar | Resize | Maximize | DoubleClicks);
  150.     set_winfns(w, &close, &resize, &redraw);
  151.     show_window(w);
  152.  
  153.     set_mouse_delay(1000);
  154.  
  155.     while(1) {
  156.         if (can_mouse(w)) {
  157.             m = get_mouse(w);
  158.             printmouse(w, m);
  159.         }
  160.         if (can_key(w)) {
  161.             c = get_key(w);
  162.             printchar(w, c);
  163.         }
  164.     }
  165.  
  166.     return 0;
  167. }
  168.